package cn.tedu.passort.dao.persist.repository.impl;


import cn.tedu.passort.dao.persist.mapper.JobApplicantMapper;
import cn.tedu.passort.dao.persist.mapper.RecruiterDetailsMapper;
import cn.tedu.passort.dao.persist.mapper.UserMapper;
import cn.tedu.passort.dao.persist.mapper.UserRoleMapper;
import cn.tedu.passort.dao.persist.repository.IUserRepository;
import cn.tedu.passort.pojo.entity.JobApplicant;
import cn.tedu.passort.pojo.entity.RecruiterDetails;
import cn.tedu.passort.pojo.entity.User;
import cn.tedu.passort.pojo.entity.UserRole;
import cn.tedu.passort.pojo.param.AddJobApplicantParam;
import cn.tedu.passort.pojo.param.AddRecruiterParam;
import cn.tedu.passort.pojo.param.RegParam;
import cn.tedu.passort.pojo.vo.UserLoginInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserRepository implements IUserRepository {
@Autowired
private UserMapper userMapper;
@Autowired
private JobApplicantMapper jobApplicantMapper;
    @Override
    public int count(String phone) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        return userMapper.selectCount(wrapper);
    }

    @Override
    public void reg(String phone) {
        User user=new User();
        user.setPhone(phone);
        user.setIsEnable(1);

        userMapper.insert(user);

    }
@Autowired
private UserRoleMapper userRoleMapper;
    @Override
    public void reg(String phone, Long roleId) {
        User user=new User();
        user.setPhone(phone);
        user.setIsEnable(1);
        userMapper.insert(user);
        UserRole userRole=new UserRole();
       Long userId= userMapper.getIdByPhone(phone);
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleMapper.insert(userRole);
    }

    @Override
    public UserLoginInfoVO getUser(String phone) {
       return userMapper.getUser(phone);

    }

    @Override
    public int updateLoginNum(String phone, Integer loginNum) {
        userMapper.updateLoginNum(phone,loginNum);
        return 0;
    }

    @Override
    public Long getIdByPhone(String phone) {
        return userMapper.getIdByPhone(phone);
    }

    @Override
    public List<String> getPermission(String phone) {
        return userMapper.getPermission(phone);
    }


    @Override
    public void add(AddJobApplicantParam addJobApplicantParam) {
        //添加到求职者详情
        JobApplicant jobApplicant=new JobApplicant();
        BeanUtils.copyProperties(addJobApplicantParam,jobApplicant);
        jobApplicantMapper.insert(jobApplicant);
        //修改用户表
        User user=new User();
        user.setId(addJobApplicantParam.getUserId());
        user.setUsername(addJobApplicantParam.getUsername());
        user.setAvatar(addJobApplicantParam.getUserUrl());
        user.setEmail(addJobApplicantParam.getEmail());
        user.setLoginNum(1);
        userMapper.updateById(user);

    }

    @Autowired
    private RecruiterDetailsMapper recruiterDetailsMapper;
    @Override
    public void add(AddRecruiterParam addRecruiterParam) {
        //添加招聘者详情
        RecruiterDetails recruiterDetails=new RecruiterDetails();
        recruiterDetails.setUsername(addRecruiterParam.getUsername());
        recruiterDetails.setUserId(addRecruiterParam.getUserId());
        recruiterDetails.setPhoneNumber(addRecruiterParam.getPhoneNumber());
        recruiterDetails.setRoleId(recruiterDetails.getRoleId());
        recruiterDetails.setGender(addRecruiterParam.getGender());
        recruiterDetails.setAge(addRecruiterParam.getAge());
        recruiterDetailsMapper.insert(recruiterDetails);

        //修改用户信息
        User user=new User();
        user.setId(addRecruiterParam.getUserId());
        user.setUsername(addRecruiterParam.getUsername());
        user.setAvatar(addRecruiterParam.getAvatar());
        user.setEmail(addRecruiterParam.getEmail());
        user.setLoginNum(1);
        userMapper.updateById(user);
    }
}
